package com.hp.ppm.ext.kuka;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.commons.dbutils.QueryRunner;

import com.config.PropertiesConfig;
import com.config.PropertiesService;
import com.hp.ppm.ext.service.UserCache;
import com.kintana.core.util.PasswordManager;
import com.kintana.sc.security.ElGamalPrivateKey;
import com.kintana.sc.security.ElGamalPublicKey;
import com.utils.DbBuilder;

public class LoginCheck {
	
	PropertiesConfig config = PropertiesService.getApplicationConfig();
	
	
	public String checkUser(String username,String pwd){
		String result="false"; 
		try {
			 PasswordManager.setPublicKey(new ElGamalPublicKey(config.getProperty("public.key")));
			 PasswordManager.setPrivateKey(new ElGamalPrivateKey(config.getProperty("private.key")));
			 
			 Object[] arrayResult = DbBuilder.getFirstRowArray("select USERNAME,PASSWORD from KNTA_USERS where USERNAME='"+username+"'");
			 QueryRunner runner = DbBuilder.getQueryRunner();
			 if(arrayResult.length==0){
				 result="登录用户不存在";
			 }else{
				String dbpwd=PasswordManager.decrypt(arrayResult[1].toString());
				if(pwd.equals(dbpwd)){
					result="true";
				}else{
					 result="登录用户密码错误";
				}
			 }

		} catch (Exception e) {
			result="登录异常 :"+e.getMessage();
			e.printStackTrace();
					
		}
		if(result.equals("true")) {
			UserCache.getInstancei().put(username, pwd);
		}
		 
		return result;
	}
	 
	 public static void main(String[] args) throws IOException {
		 
		 LoginCheck loginCheck=new LoginCheck();
		 System.out.println(loginCheck.checkUser("zhoukun", "2323"));
		 PasswordManager.setPublicKey(new ElGamalPublicKey("C:/Users/hunk/workspace/ppmext/src/security/public_key.txt"));
		 PasswordManager.setPrivateKey(new ElGamalPrivateKey("C:/Users/hunk/workspace/ppmext/src/security/private_key.txt"));
		 String xxx=PasswordManager.decrypt("9u~b4T zvEYC?&4y*4r:PBtOiOgMpE7eLb<&LeM=iqEK6&qZbJW-4o>2dDGYe~u5v-joCv&(W1~0kklTa=KZ*=B_B21>|A,8t2(7zm@zZiPmQp}D2rV{Eoj{Gd+yQSuKqWAj}lK*l@IJF=/`kOoWiUc*N3tYD>a/x$ub^(BG<UCzHtx &~cja43i55p~ ");
		// System.out.println(PasswordManager.encrypt("111111","DES"));

		System.out.println(PasswordManager.decrypt("9u~b4T zvEYC?&4y*4r:PBtOiOgMpE7eLb<&LeM=iqEK6&qZbJW-4o>2dDGYe~u5v-joCv&(W1~0kklTa=KZ*=B_B21>|A,8t2(7zm@zZiPmQp}D2rV{Eoj{Gd+yQSuKqWAj}lK*l@IJF=/`kOoWiUc*N3tYD>a/x$ub^(BG<UCzHtx &~cja43i55p~ ","ElGamal"));
	}

}
